package com.example.char04;

import android.content.Context;

/**
 * Created by Administrator on 2017/6/22.
 */

public class DensityUtil {
    public static int dip2px(Context context, int dpValue) {
        try {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.55);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return (int) dpValue;

    }

    /**
     * 像素px转换为dip
     */
    public static int dip2px(Context context, float pxValue) {
        try {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (pxValue * scale + 0.55);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return (int) pxValue;
    }
}